home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / iname.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  9KB  |  277 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* iname.c */
  20. /* Name lookup for Ghostscript interpreter */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "alloc.h"
  24. #include "errors.h"
  25. #include "iname.h"
  26. #include "ivmspace.h"
  27. #include "store.h"
  28.  
  29. /* Definitions and structure for the name table. */
  30. /* The first entry is left unused. */
  31. /* 1-character names are the next nt_1char_size entries. */
  32. #define nt_log2_sub_size 7
  33. #define nt_sub_size (1 << nt_log2_sub_size)
  34. #define nt_sub_index_mask (nt_sub_size - 1)
  35. #define nt_hash_size 256        /* must be a power of 2 */
  36. #define nt_1char_size 128
  37. typedef name name_sub_table[nt_sub_size];
  38. typedef struct {
  39.     ushort hash[nt_hash_size];
  40.     name *table[1 << (16 - nt_log2_sub_size)];    /* name_sub_table */
  41.     ref count;            /* t_integer */
  42. #define nt_count(nt) (uint)((nt)->count.value.intval)
  43. #define set_nt_count(nt,cnt) ((nt)->count.value.intval = (cnt))
  44. } name_table;
  45. #define name_index_ptr(nt, index)\
  46.   ((nt)->table[(index) >> nt_log2_sub_size] + ((index) & nt_sub_index_mask))
  47.  
  48. /*
  49.  * Scramble the assignment order within a sub-table, so that
  50.  * dictionary lookup doesn't have to scramble the index.
  51.  * The algorithm must have three properties:
  52.  *    - It must map 0 to 0;
  53.  *    - It must only scramble the sub-table index;
  54.  *    - It must be a permutation on the sub-table index.
  55.  * We do something very simple for now.
  56.  */
  57. #define name_count_to_index(cnt)\
  58.   (((cnt) & (-nt_sub_size)) + (((cnt) * 59) & nt_sub_index_mask))
  59. /* We also store the reverse permutation, for age checking in restore. */
  60. #define name_index_to_count(idx)\
  61.   ((idx) ^ name_sub_index_to_count[(idx) & nt_sub_index_mask])
  62. #if nt_sub_size <= 256
  63. private byte name_sub_index_to_count[nt_sub_size];
  64. #else
  65. private ushort name_sub_index_to_count[nt_sub_size];
  66. #endif
  67.  
  68. /* The one and only name table (for now). */
  69. private name_table *the_nt;
  70.  
  71. /* Forward references */
  72. private int name_alloc_sub(P1(name_table *));
  73.  
  74. /* Make a t_name ref out of a name * */
  75. #define make_name(pref, idx, pnm)\
  76.   make_tasv(pref, t_name, 0, idx, pname, pnm)
  77.  
  78. /* Initialize the name table */
  79. void
  80. name_init(void)
  81. {    register uint i;
  82.     static byte one_char_names[nt_1char_size];
  83.     uint local = alloc_select_local(0);
  84.     /* Initialize the index_to_count map. */
  85.     for ( i = 0; i < nt_sub_size; i++ )
  86.        {    uint idx = name_count_to_index(i);
  87.         name_sub_index_to_count[idx] = idx ^ i;
  88.        }
  89.     the_nt = (name_table *)alloc(1, sizeof(name_table), "name_init");
  90.     memset(the_nt, 0, sizeof(name_table));
  91.     make_int(&the_nt->count, 1);
  92.     /* Initialize the one-character names. */
  93.     for ( i = 0; i < nt_1char_size; i++ )
  94.        {    uint ncnt = i + 1;
  95.         uint nidx = name_count_to_index(ncnt);
  96.         register name *pname;
  97.         if ( the_nt->table[nidx >> nt_log2_sub_size] == 0 )
  98.             name_alloc_sub(the_nt);
  99.         pname = name_index_ptr(the_nt, nidx);
  100.         set_nt_count(the_nt, ncnt + 1);
  101.         one_char_names[i] = i;
  102.         pname->string_size = 1;
  103.         pname->string_bytes = &one_char_names[i];
  104.         pname->pvalue = pv_no_defn;
  105.        }
  106.     alloc_select_local(local);
  107. }
  108.  
  109. /* Look up or enter a name in the table. */
  110. /* Return 0 or an error code. */
  111. /* The return may overlap the characters of the string! */
  112. /* See iname.h for the meaning of enterflag. */
  113. int
  114. name_ref(const byte *ptr, uint isize, ref *pref, int enterflag)
  115. {    register name *pname;
  116.     const byte *cptr;
  117.     ushort size = (ushort)isize;    /* see iname.h */
  118.     uint nidx;
  119.     if ( size == 1 && *ptr < nt_1char_size )
  120.        {    uint ccnt = *ptr + 1;
  121.         nidx = name_count_to_index(ccnt);
  122.         pname = name_index_ptr(the_nt, nidx);
  123.         make_name(pref, nidx, pname);
  124.         return 0;
  125.        }
  126.     else
  127.        {    ushort *phash =
  128.           the_nt->hash +
  129.             ((ushort)string_hash(ptr, size) & (nt_hash_size - 1));
  130.         uint ncnt;
  131.         nidx = *phash;
  132.         while ( nidx != 0 )
  133.            {    pname = name_index_ptr(the_nt, nidx);
  134.             if ( pname->string_size == size &&
  135.                  !memcmp(ptr, pname->string_bytes, size)
  136.                )
  137.                {    make_name(pref, nidx, pname);
  138.                 return 0;
  139.                }
  140.             nidx = pname->next_index;
  141.            }
  142.         /* Not in table, allocate a new entry. */
  143.         if ( enterflag < 0 )
  144.             return_error(e_undefined);
  145.         if ( !(nt_count(the_nt) & (nt_sub_size - 1)) )
  146.            {    int code = name_alloc_sub(the_nt);
  147.             if ( code < 0 ) return code;
  148.            }
  149.         ncnt = nt_count(the_nt);
  150.         nidx = name_count_to_index(ncnt);
  151.         pname = name_index_ptr(the_nt, nidx);
  152.         pname->next_index = *phash;
  153.         *phash = nidx;
  154. #ifdef DEBUG
  155.         if ( debug_c('n') )
  156.         {    uint ci;
  157.             dprintf4("[n]new name 0x%lx#%u, length=%u, count=%u: ",
  158.                  (ulong)pname, nidx, isize, ncnt);
  159.             for ( ci = 0; ci < isize; ci++ )
  160.                 dputc(ptr[ci]);
  161.             dputc('\n');
  162.         }
  163. #endif
  164.         ref_save(&the_nt->count, "name_ref(count)");
  165.         set_nt_count(the_nt, ncnt + 1);
  166.        }
  167.     /* Name was not in the table.  Make a new entry. */
  168.     if ( enterflag )
  169.     {    uint local = alloc_select_local(0);
  170.         cptr = (const byte *)alloc(size, 1, "name_ref(string)");
  171.         alloc_select_local(local);
  172.         if ( cptr == 0 )
  173.             return_error(e_VMerror);
  174.         memcpy((byte *)cptr, ptr, size);
  175.     }
  176.     else
  177.         cptr = ptr;
  178.     pname->string_size = size;
  179.     pname->string_bytes = cptr;
  180.     pname->pvalue = pv_no_defn;
  181.     make_name(pref, nidx, pname);
  182.     return 0;
  183. }
  184.  
  185. /* Get the string for a name. */
  186. void
  187. name_string_ref(const ref *pnref /* t_name */,
  188.   ref *psref /* result, t_string */)
  189. {    name *pname = pnref->value.pname;
  190.     make_const_string(psref, a_readonly, pname->string_size,
  191.               (const byte *)pname->string_bytes);
  192. }
  193.  
  194. /* Convert a t_string object to a name. */
  195. /* Copy the executable attribute. */
  196. int
  197. name_from_string(const ref *psref, ref *pnref)
  198. {    int exec = r_has_attr(psref, a_executable);
  199.     int code = name_ref(psref->value.bytes, r_size(psref), pnref, 1);
  200.     if ( code < 0 ) return code;
  201.     if ( exec ) r_set_attrs(pnref, a_executable);
  202.     return code;
  203. }
  204.  
  205. /* Enter a name during initialization. */
  206. /* Fatal error if the entry fails. */
  207. void
  208. name_enter(const char *str, ref *pref)
  209. {    if ( name_ref((const byte *)str, strlen(str), pref, 0) )
  210.         lprintf1("name_enter failed - %s", str),
  211.         gs_exit(1);
  212. }
  213.  
  214. /* Get the name with a given index. */
  215. void
  216. name_index_ref(uint index, ref *pnref)
  217. {    make_name(pnref, index, name_index_ptr(the_nt, index));
  218. }
  219.  
  220. /* Get the current name count. */
  221. uint
  222. name_count(void)
  223. {    return nt_count(the_nt);
  224. }
  225.  
  226. /* Check whether a name was created since a given count. */
  227. int
  228. name_is_since_count(const ref *pnref, uint cnt)
  229. {    return name_index_to_count(name_index(pnref)) >= cnt;
  230. }
  231.  
  232. /* Clean up the name table before a restore. */
  233. /* The count will be reset, and added subtables will be freed. */
  234. /* All we have to do is remove initial entries from the hash chains, */
  235. /* since we know they are linked in decreasing index order */
  236. /* (by sub-table, but not within each sub-table.) */
  237. /* (There will be some spurious non-zero entries in the subtable table, */
  238. /* but this doesn't matter since they will never be accessed.) */
  239. void
  240. name_restore(uint old_count)
  241. {    ushort *phash = &the_nt->hash[0];
  242.     uint old_sub = old_count & -nt_sub_size;
  243.     register uint i;
  244.     for ( i = 0; i < nt_hash_size; phash++, i++ )
  245.        {    register ushort *pnh = phash;
  246.         while ( *pnh >= old_sub )
  247.            {    if ( name_index_to_count(*pnh) < old_count )
  248.               pnh = &name_index_ptr(the_nt, *pnh)->next_index;
  249.             else
  250.                {
  251. #ifdef DEBUG
  252.                 if ( debug_c('n') | debug_c('U') )
  253.                   dprintf2("[n]restore remove name 0x%lx#%u\n",
  254.                        (ulong)name_index_ptr(the_nt, *pnh),
  255.                        (uint)*pnh);
  256. #endif
  257.                 *pnh = name_index_ptr(the_nt, *pnh)->next_index;
  258.                }
  259.            }
  260.        }
  261. }
  262.  
  263. /* ------ Internal procedures ------ */
  264.  
  265. /* Allocate the next sub-table. */
  266. private int
  267. name_alloc_sub(name_table *nt)
  268. {    uint local = alloc_select_local(0);
  269.     name *sub = (name *)alloc(1, sizeof(name_sub_table), "name_alloc_sub");
  270.     alloc_select_local(local);
  271.     if ( sub == 0 )
  272.         return_error(e_VMerror);
  273.     memset(sub, 0, sizeof(name_sub_table));
  274.     nt->table[nt_count(nt) >> nt_log2_sub_size] = sub;
  275.     return 0;
  276. }
  277.